package Model.DateProfile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Model.User;

import org.json.*;

/**
 * Servlet implementation class RegistrationServlet
 */
@WebServlet("/questionnaire")
public class QuestionnaireServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Properties props = new Properties();

	@Override
	public void init() {
		InputStream in = this.getServletContext().getResourceAsStream(
				"/config.properties");
		try {
			props.load(in);
		} catch (Exception e) {
			System.err
					.println("Unrecoverable error. Could not load configuration.");
			
		}
	}

	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//fields for personaldb
		String userIDString = request.getParameter("userID");
		int userID = 0;
		if (userIDString != null) {
			userID = Integer.parseInt(userIDString);
		}
		
		String address = request.getParameter("myAddress");
		if (address == null){
			address = "";
		}
		
		String heightString = request.getParameter("myHeight");
		int height = 0;
		if (heightString != null && !heightString.equals("")) {
			height = Integer.parseInt(heightString);
		} 
		
		String weightString = request.getParameter("myWeight");
		int weight = 0;
		if(heightString != null) {
		weight = Integer.parseInt(weightString);
		}
		
		String bMonthString = request.getParameter("bMonth");
		int bMonth = 1;
		if(bMonthString !=null) {
		bMonth = Integer.parseInt(bMonthString);
		}
		
		String bDateString = request.getParameter("bDate");
		int bDate = 1;
		if(bDateString !=null) {
		bDate = Integer.parseInt(bDateString);
		}
		
		String bYearString = request.getParameter("bYear");
		int bYear = 88;
		if (bYearString != null) {
		bYear = Integer.parseInt(bYearString);
		}
		
		String interests[] = request.getParameterValues("interest");
		
		String interestsString = null;
		
		if (interests == null) {
			interestsString = "You have not listed any interests...";
		}
		
		if(interests != null){
			StringBuffer buf = new StringBuffer();
			for (int i=0; i<interests.length; i++){
				buf.append(interests[i]+";");
			}
			interestsString = buf.toString();
		}
		
		String myGenderString = request.getParameter("myGender");
		int myGender = 0;
		if (myGenderString != null){
		myGender = Integer.parseInt(myGenderString);
		}
		
		String myOrientationString = request.getParameter("myOrientation");
		int myOrientation = 1;
		if (myOrientationString != null) {
		myOrientation = Integer.parseInt(myOrientationString);
		}
		
		int myNewsletterFlag = 0;
		
		int myNotificationFlag = 0;
		
		String myEthnicity = request.getParameter("myEthnicity");
		String myReligion = request.getParameter("myReligion");
		
		//field for preferencedb
		String ageLowerPrefString = request.getParameter("ageLowerPref");
		int ageLowerPref = 20;
		if(ageLowerPrefString != null) {
		ageLowerPref = Integer.parseInt(ageLowerPrefString);
		}
		
		String ageHigherPrefString = request.getParameter("ageHigherPref");
		int ageHigherPref = 99;
		if (ageHigherPrefString != null) {
		ageHigherPref = Integer.parseInt(ageLowerPrefString);
		}
		
		String heightLowerPrefString = request.getParameter("heightLowerPref");
		int heightLowerPref = 0;
		if (heightLowerPrefString != null) {
		heightLowerPref = Integer.parseInt(heightLowerPrefString);
		}
		
		String heightHigherPrefString = request.getParameter("heightHigherPref");
		int heightHigherPref = 100;
		if(heightHigherPrefString != null) {
		heightHigherPref = Integer.parseInt(heightHigherPrefString);
		}
		
		String weightLowerPrefString = request.getParameter("weightLowerPref");
		int weightLowerPref = 0;
		if(weightLowerPrefString != null) {
		weightLowerPref = Integer.parseInt(weightLowerPrefString);
		}
		
		String weightHigherPrefString = request.getParameter("weightHigherPref");
		int weightHigherPref = 100;
		if (weightHigherPrefString != null) {
		weightHigherPref = Integer.parseInt(weightHigherPrefString);
		}
		
		String ethnicityPref = request.getParameter("ethnicityPref");
		
		String religionPref = request.getParameter("religionPref");
		
		
		request.setAttribute("tryingAgain", false);
		RequestDispatcher dispatcher;

		String referer = request.getHeader("Referer");
		System.out.println(referer);
		referer = "http://localhost:8080/UBCDatingServer/profile.jsp";


		int valid1 = Questionnaire.personalInput(userID, address, height, weight, bYear, bMonth, bDate, interestsString, myGender, myOrientation, myNewsletterFlag, myNotificationFlag, myReligion, myEthnicity);
		int valid2 = Questionnaire.preferenceInput(userID, weightLowerPref, weightHigherPref, heightLowerPref, heightHigherPref, ageLowerPref, ageHigherPref, ethnicityPref, religionPref, 1);
		if (valid1==valid2 && valid1 >= 0) {
			HttpSession session = request.getSession();
			System.out.println(valid1);
			if (session != null) {
				session.setAttribute("loggedIn", true);
				session.setAttribute("userID", valid1);
	
				User myU = new User(valid1);
				JSONObject myJson = new JSONObject(myU);
				System.out.print(myJson);
				session.setAttribute("userInfo", myJson);
				session.setAttribute("name", myU.getName());

				// response.sendRedirect(referer);
				String originalTarget = (String) session
						.getAttribute("originalTarget");
				if (originalTarget != null) {
					response.sendRedirect(originalTarget);
					return;
				}
			}
			// dispatcher = request.getRequestDispatcher(referer);
			response.sendRedirect(referer);

		} else {
			request.setAttribute("tryingAgain", true);
			// dispatcher = request.getRequestDispatcher(referer);
			response.sendRedirect(referer);
		}
	}

}
